﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public class ChangePropertyOperation : OperationBase
    {
        private Coolzon.UI.XProperty.XPropertyValueEditor _editor;
        private string _xPropertyName;
        private System.Windows.DependencyProperty _propertyObject;
        private Type _propertyType;
        private object _oldValue;
        private object _newValue;
        public ChangePropertyOperation(Coolzon.UI.XProperty.XPropertyValueEditor editor, string xPropertyName, System.Windows.DependencyProperty propertyObject, Type propertyType, object oldValue, object newValue)
        {
            _editor = editor;
            _xPropertyName = xPropertyName;
            _propertyObject = propertyObject;
            _propertyType = propertyType;
            _oldValue = oldValue;
            _newValue = newValue;
        }
        protected override void OnDo()
        {
            try
            {
                _editor.SetValue(_propertyObject, _newValue);
                Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_editor.PartInstance.Metadata, _xPropertyName, _propertyType, _newValue);
                if (_editor.PartInstance is Coolzon.UI.ScenePart)
                {
                    UIContext.Current.CurrentDesignView.API_ReOpenScene();
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag(Library.LogTAG, "编辑器{0}更新组件{1}的属性{2}出错,ex:{3}", _editor.GetType().FullName, _editor.PartInstance.Name, _xPropertyName, ex);
            }
        }

        protected override void OnUndo()
        {
            try
            {
                _editor.SetValue(_propertyObject, _oldValue);
                Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_editor.PartInstance.Metadata, _xPropertyName, _propertyType, _oldValue);
                if (_editor.PartInstance is Coolzon.UI.ScenePart)
                {
                    UIContext.Current.CurrentDesignView.API_ReOpenScene();
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag(Library.LogTAG, "编辑器{0}撤销组件{1}的属性{2}出错,ex:{3}", _editor.GetType().FullName, _editor.PartInstance.Name, _xPropertyName, ex);
            }
        }
    }
}
